import { SUBSCRIBE } from '../config.js'

const state = {
	subscribe: uni.getStorageSync(SUBSCRIBE) || [] //订阅列表
}

const getters = {
	get (state) {
		return state.subscribe
	}
}

const mutations = {
	set (state, subscribe) {
		state.subscribe = subscribe
		uni.setStorageSync(SUBSCRIBE, state.subscribe)
	}
}

const actions = {
	save ({state, commit}, id) {
		const subscribe = [...state.subscribe]
		const index = subscribe.indexOf(id)
		if ( index == -1 ) subscribe.push(id)
		commit('set', subscribe)
	},
	remove ({state, commit}, id) {
		const subscribe = [...state.subscribe]
		const index = subscribe.indexOf(id)
		if ( index > -1 ) subscribe.splice(index, 1);
		commit('set', subscribe)
	},
	switch ({state, commit}, id) {
		const subscribe = [...state.subscribe]
		const index = subscribe.indexOf(id)
		if ( index > -1 ) { subscribe.splice(index, 1) }
		else { subscribe.push(id) }
		commit('set', subscribe)
	},
	clear ({commit}, type) {
		commit('set', [])
	}
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
	actions
}